#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSettings>

#define HKEY_CURRENT_USER_QT "HKEY_CURRENT_USER\\SoftWare\\Digia\\Qt"
//const QString HKEY_CURRENT_USER_QT = "HKEY_CURRENT_USER\\SoftWare\\Digia\\Qt";
#define INI_QT "C:\Users\FG\AppData\Roaming\Digia"
//const QString INI_QT = "C:\Users\FG\AppData\Roaming\Digia";


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle(tr("QSetting读写"));
//  QCoreApplication::setOrganizationName(QString("Digia"));
//  QCoreApplication::setApplicationName(QString("Qt"));

//  读取和存储注册表
//  writeRgister();
//  readRegedit();
//  SubDirectoryWriteRegedit();

//  读取和存储配置文件
//  WriteIniConfig();
//  ReadIniConfig();

//  删除配置文件部分
    DeleteIniConfig();
}


void MainWindow::writeRegedit()
{
    QSettings settings(QSettings::NativeFormat, QSettings::UserScope, QCoreApplication::organizationName(), QCoreApplication::applicationName());
    settings.setValue("Name", "Qt Creator");
    settings.setValue("Version", 5);
}

void MainWindow::SubDirectoryWriteRegedit()
{
//    //替换applicationName
//    QSettings settings(QSettings::NativeFormat, QSettings::UserScope, QString("%1\\%2").arg(QCoreApplication::organizationName()).arg(QCoreApplication::applicationName()), "Qt5.5");
//    settings.setValue("Name", "Qt Creator");
//    settings.setValue("Version", "5.5");
//    QSettings settings2(QString("%1\\%2").arg(QCoreApplication::organizationName()).arg(QCoreApplication::applicationName()), "Qt5.6");
//    settings2.setValue("Name", "Qt Creator");
//    settings2.setValue("Version", "5.6");
    //分组
    QSettings settings;
    settings.beginGroup("Qt5.5");
    settings.setValue("Name", "Qt Creator");
    settings.setValue("Version", "5.7");
    settings.endGroup();
    settings.beginGroup("Qt5.6");
    settings.setValue("Name", "Qt Creator");
    settings.setValue("Version", "5.8");
    settings.endGroup();
}

void MainWindow::WriteIniConfig()
{
//    QSettings settings(QSettings::IniFormat, QSettings::UserScope, QCoreApplication::organizationName(), QCoreApplication::applicationName());
    //一般存储
//    settings.setValue("Name", "Qt Creator");
//    settings.setValue("Version", 5);
    //分组存储
//    settings.beginGroup("Setting");
//    settings.setValue("Name", "Qt Creator");
//    settings.setValue("Version", 5);
//    settings.endGroup();
//    settings.beginGroup("User");
//    settings.setValue("UserName", "WangL");
//    settings.setValue("Password", "123456");
//    settings.endGroup();
    //分目录存储
    QSettings settings(QSettings::IniFormat, QSettings::UserScope, QString("%1\\%2\\%3").arg(QCoreApplication::organizationName()).arg(QCoreApplication::applicationName()).arg("Qt5.5"), "User");
    settings.setValue("Name", "Qt Creator");
    settings.setValue("Version", "5.5");
    QSettings settings2(QSettings::IniFormat, QSettings::UserScope, QString("%1\\%2\\%3").arg(QCoreApplication::organizationName()).arg(QCoreApplication::applicationName()).arg("Qt5.5"), "User");
    settings2.setValue("Name", "Qt Creator");
    settings2.setValue("Version", "5.6");
}

void MainWindow::ReadIniConfig()
{
    QSettings settings(QSettings::IniFormat, QSettings::UserScope, QCoreApplication::organizationName(), QCoreApplication::applicationName());
    //一般读取
//    QString strName = settings.value("Name").toString();
//    int nVersion = settings.value("Version").toInt();
//    ui->label->setText(strName + ":" + QString::number(nVersion,10));
    //分组读取
    settings.beginGroup("Setting");
    QString strName = settings.value("Name").toString();
    int nVersion = settings.value("Version").toInt();
    settings.endGroup();
    //Name:Qt Creator  Version:5
    settings.beginGroup("User");
    QString strUserName = settings.value("UserName").toString();
    QString strPassword = settings.value("Password").toString();
    settings.endGroup();
    //UserName:WangL  Password:123456
    ui->label->setText(strName + ":" + QString::number(nVersion,10)
                       + "  " + strUserName + ":" + strPassword);
}

void MainWindow::DeleteIniConfig()
{
    QSettings settings(QCoreApplication::applicationDirPath() + "\\conf.ini", QSettings::IniFormat);
//  settings.setValue("Name", "Qt Creator");
//  settings.setValue("Version", 5);
//  settings.remove("Name");//删除一个指定的键
//  settings.clear();//清空所有键

//  删除设置键以及子设置键
//    settings.setValue("Qt5.6", "5.6");
//    settings.beginGroup("Qt5.5");
//    settings.setValue("Name", "Qt Creator");
//    settings.setValue("Version", "5.5");
//    settings.endGroup();
//    settings.beginGroup("Qt5.6");
//    settings.setValue("Name", "Qt Creator");
//    settings.setValue("Version", "5.6");
//    settings.endGroup();
//    settings.remove("Qt5.6");

//    如果key为空字符串，在当前group()的所有键将被删除。
    settings.setValue("Qt5.6", "5.6");

    settings.beginGroup("Qt5.5");
    settings.setValue("Name", "Qt Creator");
    settings.setValue("Version", "5.5");
    settings.endGroup();

    settings.beginGroup("Qt5.6");
    settings.setValue("Name", "Qt Creator");
    settings.setValue("Version", "5.6");
    settings.endGroup();

    settings.beginGroup("Qt5.6");
    settings.remove("");
    settings.endGroup();
}

void MainWindow::readRegedit()
{
    QSettings settings(QSettings::NativeFormat, QSettings::UserScope, QCoreApplication::organizationName(), QCoreApplication::applicationName());
    QString strName = settings.value("Name").toString();
    QString version  = settings.value("Version").toString();
    ui->label->setText(strName + ":" + version);
}

MainWindow::~MainWindow()
{
    delete ui;
}

